<!DOCTYPE html>
<html lang="en-us" dir="ltr">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='pyqt的使用 基础目录结构 “firstDemo.pro”是项目管理文件，包括一些对项目的设置项。 Headers 分组，该节点下是项目内的所有头文件（.h），有一个头文件 mainwindow.h，是主窗口类的头文件。 Sources 分组：该节点下是项目内的所有 C&#43;&#43;源文件（.cpp），项目有两个 C&#43;&#43; 源文件，mainwindow.cpp 是主窗口类的实现文件，与 mainwindow.h 文件对应。main.cpp 是主函数文件，也是应用程序的入口。 Forms 分组：该节点下是项目内的所有界面文件（.ui）。有一个界面文件mainwindow.ui，是主窗口的界面文件。
模态对话框和非模态对话框 模态对话框在没有关闭之前，用户无法与同一个应用程序的其他窗口进行交互操作。 在Qt中使用QDialog::exec()函数，把一个对话框变成模态对话框。
1 2 3 4 5 6 //模态对话框 QDialog dialog(this); dialog.exec(); //非模态对话框 QDialog *dialog = new Qdialog(this); dialog-&amp;gt;show(); 信号与槽 QObject::connect(sender, signal, receiver, slot); sender：发出信号的对象 signal：发送对象(即sender)发出的信号 receiver：接收信号的对象 slot：接收对象(即receiver)在接收到信号之后所需要调用的函数
CV笔记 编译opencv modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1 30%处出现此错误 不勾选 OPENCV_ENABLE_ALLOCATOR_STATS 后暂时解决,会在60%后继续出现 勾选后 继续出现 更换cmake版本为3.16.2 解决 Compilation of OpenCV opencv_test_photo error D:\Opencv\opencv\opencv\sources\modules\photo\test\test_hdr.cpp and place this statement where the header declarations are: #include &amp;lt;ctime&amp;gt;'><title>opencv和QT</title>

<link rel='canonical' href='http://meruro.cn/p/opencv%E5%92%8Cqt/'>

<link rel="stylesheet" href="/scss/style.min.5a8e892f6fa14515e9065eee1f5d2e05302606ec7f11750b2fb95198f9cdcbfd.css"><meta property='og:title' content='opencv和QT'>
<meta property='og:description' content='pyqt的使用 基础目录结构 “firstDemo.pro”是项目管理文件，包括一些对项目的设置项。 Headers 分组，该节点下是项目内的所有头文件（.h），有一个头文件 mainwindow.h，是主窗口类的头文件。 Sources 分组：该节点下是项目内的所有 C&#43;&#43;源文件（.cpp），项目有两个 C&#43;&#43; 源文件，mainwindow.cpp 是主窗口类的实现文件，与 mainwindow.h 文件对应。main.cpp 是主函数文件，也是应用程序的入口。 Forms 分组：该节点下是项目内的所有界面文件（.ui）。有一个界面文件mainwindow.ui，是主窗口的界面文件。
模态对话框和非模态对话框 模态对话框在没有关闭之前，用户无法与同一个应用程序的其他窗口进行交互操作。 在Qt中使用QDialog::exec()函数，把一个对话框变成模态对话框。
1 2 3 4 5 6 //模态对话框 QDialog dialog(this); dialog.exec(); //非模态对话框 QDialog *dialog = new Qdialog(this); dialog-&amp;gt;show(); 信号与槽 QObject::connect(sender, signal, receiver, slot); sender：发出信号的对象 signal：发送对象(即sender)发出的信号 receiver：接收信号的对象 slot：接收对象(即receiver)在接收到信号之后所需要调用的函数
CV笔记 编译opencv modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1 30%处出现此错误 不勾选 OPENCV_ENABLE_ALLOCATOR_STATS 后暂时解决,会在60%后继续出现 勾选后 继续出现 更换cmake版本为3.16.2 解决 Compilation of OpenCV opencv_test_photo error D:\Opencv\opencv\opencv\sources\modules\photo\test\test_hdr.cpp and place this statement where the header declarations are: #include &amp;lt;ctime&amp;gt;'>
<meta property='og:url' content='http://meruro.cn/p/opencv%E5%92%8Cqt/'>
<meta property='og:site_name' content='梅鲁梅鲁梅鲁梅~'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='代码' /><meta property='article:published_time' content='2018-09-18T22:00:07&#43;00:00'/><meta property='article:modified_time' content='2018-09-18T22:00:07&#43;00:00'/>
<meta name="twitter:title" content="opencv和QT">
<meta name="twitter:description" content="pyqt的使用 基础目录结构 “firstDemo.pro”是项目管理文件，包括一些对项目的设置项。 Headers 分组，该节点下是项目内的所有头文件（.h），有一个头文件 mainwindow.h，是主窗口类的头文件。 Sources 分组：该节点下是项目内的所有 C&#43;&#43;源文件（.cpp），项目有两个 C&#43;&#43; 源文件，mainwindow.cpp 是主窗口类的实现文件，与 mainwindow.h 文件对应。main.cpp 是主函数文件，也是应用程序的入口。 Forms 分组：该节点下是项目内的所有界面文件（.ui）。有一个界面文件mainwindow.ui，是主窗口的界面文件。
模态对话框和非模态对话框 模态对话框在没有关闭之前，用户无法与同一个应用程序的其他窗口进行交互操作。 在Qt中使用QDialog::exec()函数，把一个对话框变成模态对话框。
1 2 3 4 5 6 //模态对话框 QDialog dialog(this); dialog.exec(); //非模态对话框 QDialog *dialog = new Qdialog(this); dialog-&amp;gt;show(); 信号与槽 QObject::connect(sender, signal, receiver, slot); sender：发出信号的对象 signal：发送对象(即sender)发出的信号 receiver：接收信号的对象 slot：接收对象(即receiver)在接收到信号之后所需要调用的函数
CV笔记 编译opencv modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1 30%处出现此错误 不勾选 OPENCV_ENABLE_ALLOCATOR_STATS 后暂时解决,会在60%后继续出现 勾选后 继续出现 更换cmake版本为3.16.2 解决 Compilation of OpenCV opencv_test_photo error D:\Opencv\opencv\opencv\sources\modules\photo\test\test_hdr.cpp and place this statement where the header declarations are: #include &amp;lt;ctime&amp;gt;">
    </head>
    <body class="
    article-page
    ">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex on-phone--column extended"><aside class="sidebar left-sidebar sticky ">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="Toggle Menu">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header>
        
            
            <figure class="site-avatar">
                <a href="/">
                
                    
                    
                    
                        
                        <img src="/img/ava_hu31add2dceb44736f969f74989490346b_19511_300x0_resize_q75_box.jpg" width="300"
                            height="300" class="site-logo" loading="lazy" alt="Avatar">
                    
                
                </a>
                
                    <span class="emoji">🍥</span>
                
            </figure>
            
        
        
        <div class="site-meta">
            <h1 class="site-name"><a href="/">梅鲁梅鲁梅鲁梅~</a></h1>
            <h2 class="site-description">朝闻道,夕死可矣.</h2>
        </div>
    </header><ol class="social-menu">
            
                <li>
                    <a 
                        href='https://github.com/CaiJimmy/hugo-theme-stack'
                        target="_blank"
                        title="GitHub"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-github" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" />
</svg>



                        
                    </a>
                </li>
            
                <li>
                    <a 
                        href='https://twitter.com'
                        target="_blank"
                        title="Twitter"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-twitter" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M22 4.01c-1 .49 -1.98 .689 -3 .99c-1.121 -1.265 -2.783 -1.335 -4.38 -.737s-2.643 2.06 -2.62 3.737v1c-3.245 .083 -6.135 -1.395 -8 -4c0 0 -4.182 7.433 4 11c-1.872 1.247 -3.739 2.088 -6 2c3.308 1.803 6.913 2.423 10.034 1.517c3.58 -1.04 6.522 -3.723 7.651 -7.742a13.84 13.84 0 0 0 .497 -3.753c-.002 -.249 1.51 -2.772 1.818 -4.013z" />
</svg>



                        
                    </a>
                </li>
            
        </ol><ol class="menu" id="main-menu">
        
        
        

        <li >
            <a href='/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>Home</span>
            </a>
        </li>
        
        

        <li >
            <a href='/about/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>About</span>
            </a>
        </li>
        
        

        <li >
            <a href='/archives/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>Archives</span>
            </a>
        </li>
        
        

        <li >
            <a href='/search/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="10" cy="10" r="7" />
  <line x1="21" y1="21" x2="15" y2="15" />
</svg>



                
                <span>Search</span>
            </a>
        </li>
        
        

        <li >
            <a href='/links/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-link" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" />
  <path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" />
</svg>



                
                <span>Links</span>
            </a>
        </li>
        

        <div class="menu-bottom-section">
                <li id="i18n-switch">  
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-language" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M4 5h7" />
  <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  <path d="M5 9c-.003 2.144 2.952 3.908 6.7 4" />
  <path d="M12 20l4 -9l4 9" />
  <path d="M19.1 18h-6.2" />
</svg>



                    <select name="language" onchange="window.location.href = this.selectedOptions[0].value">
                        
                            <option value="http://meruro.cn/" selected>English</option>
                        
                            <option value="http://meruro.cn/zh-cn/" >中文</option>
                        
                            <option value="http://meruro.cn/ar/" >عربي</option>
                        
                    </select>
                </li>
            
            
            
                <li id="dark-mode-toggle">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="8" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="16" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <span>Dark Mode</span>
                </li>
            
        </div>
    </ol>
</aside>
<main class="main full-width">
    <article class="main-article">
    <header class="article-header">

    <div class="article-details">
    

    <div class="article-title-wrapper">
        <h2 class="article-title">
            <a href="/p/opencv%E5%92%8Cqt/">opencv和QT</a>
        </h2>
    
        
    </div>

    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Sep 18, 2018</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    10 minute read
                </time>
            </div>
        
    </footer>
    

    
</div>
</header>

    <section class="article-content">
    
    
    <h1 id="pyqt的使用">pyqt的使用</h1>
<h2 id="基础目录结构">基础目录结构</h2>
<p>“firstDemo.pro”是项目管理文件，包括一些对项目的设置项。
Headers 分组，该节点下是项目内的所有头文件（.h），有一个头文件 mainwindow.h，是主窗口类的头文件。
Sources 分组：该节点下是项目内的所有 C++源文件（.cpp），项目有两个 C++ 源文件，mainwindow.cpp 是主窗口类的实现文件，与 mainwindow.h 文件对应。main.cpp 是主函数文件，也是应用程序的入口。
Forms 分组：该节点下是项目内的所有界面文件（.ui）。有一个界面文件mainwindow.ui，是主窗口的界面文件。</p>
<h2 id="模态对话框和非模态对话框">模态对话框和非模态对话框</h2>
<p>模态对话框在没有关闭之前，用户无法与同一个应用程序的其他窗口进行交互操作。
在Qt中使用QDialog::exec()函数，把一个对话框变成模态对话框。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl">    <span class="c1">//模态对话框
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">QDialog</span> <span class="nf">dialog</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">dialog</span><span class="p">.</span><span class="n">exec</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="c1">//非模态对话框
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">QDialog</span> <span class="o">*</span><span class="n">dialog</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Qdialog</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">dialog</span><span class="o">-&gt;</span><span class="n">show</span><span class="p">();</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="信号与槽">信号与槽</h2>
<p><code>QObject::connect(sender, signal, receiver, slot);</code>
sender：发出信号的对象  signal：发送对象(即sender)发出的信号  receiver：接收信号的对象  slot：接收对象(即receiver)在接收到信号之后所需要调用的函数</p>
<h1 id="cv笔记">CV笔记</h1>
<h2 id="编译opencv">编译opencv</h2>
<p><code>modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1</code> 30%处出现此错误
不勾选 OPENCV_ENABLE_ALLOCATOR_STATS 后暂时解决,会在60%后继续出现
勾选后 继续出现
更换cmake版本为3.16.2 解决
Compilation of OpenCV opencv_test_photo error
D:\Opencv\opencv\opencv\sources\modules\photo\test\test_hdr.cpp and place this statement where the header declarations are:
<code>#include &lt;ctime&gt;</code></p>
<h2 id="cvmat-mat初始化">cv::Mat mat初始化</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// 默认形式
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 拷贝构造形式
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">&amp;</span> <span class="n">mat</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 指定行列范围的拷贝构造
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">&amp;</span> <span class="n">mat</span><span class="p">,</span> <span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Range</span><span class="o">&amp;</span> <span class="n">rows</span><span class="p">,</span> <span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Range</span><span class="o">&amp;</span> <span class="n">cols</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 指定ROI的拷贝构造
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">&amp;</span> <span class="n">mat</span><span class="p">,</span> <span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Rect</span><span class="o">&amp;</span> <span class="n">roi</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用多维数组中指定范围内的数据的拷贝构造
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">&amp;</span> <span class="n">mat</span><span class="p">,</span> <span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Range</span><span class="o">*</span> <span class="n">ranges</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 指定类型和大小(行列)的二维数组（注意：是行在前，列在后)
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 有初始化值的置顶类型和大小(行列)的二维数据
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">,</span> <span class="k">const</span> <span class="n">Scalar</span><span class="o">&amp;</span> <span class="n">s</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用预先存在数据定义的指定类型和大小(行列)的二维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">step</span> <span class="o">=</span> <span class="n">AUTO_STEP</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 指定大小和类型的二维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="n">cv</span><span class="o">::</span><span class="n">Size</span> <span class="n">sz</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">,</span> <span class="k">const</span> <span class="n">Scalar</span><span class="o">&amp;</span> <span class="n">s</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用预先存在的数据定义的指定大小和类型的二维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="n">cv</span><span class="o">::</span><span class="n">Size</span> <span class="n">sz</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">data</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">step</span> <span class="o">=</span> <span class="n">AUTO_STEP</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 指定类型的多维数据
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="kt">int</span> <span class="n">ndims</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span> <span class="o">*</span><span class="n">sizes</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用预先存在的数据定义的指定类型的多维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="kt">int</span> <span class="n">ndims</span><span class="p">,</span> <span class="k">const</span> <span class="kt">int</span><span class="o">*</span> <span class="n">sizes</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">,</span> <span class="kt">void</span><span class="o">*</span> <span class="n">data</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">step</span> <span class="o">=</span> <span class="n">AUTO_STEP</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用cv::Vec定义相同类型、大小为n的一维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="o">&gt;&amp;</span> <span class="n">vec</span><span class="p">,</span> <span class="kt">bool</span> <span class="o">=</span> <span class="n">copyData</span> <span class="o">=</span> <span class="nb">true</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用cv::Matx定义相同类型、大小为mxn的二维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">cv</span><span class="o">::</span><span class="n">Matx</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="o">&gt;&amp;</span> <span class="n">vec</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">copyData</span> <span class="o">=</span> <span class="nb">true</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用STL vector定义相同类型的一维数组
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&amp;</span> <span class="n">vec</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">copyData</span> <span class="o">=</span> <span class="nb">true</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用zeros()函数定义指定大小和类型的cv::Mat（全为0）
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">::</span><span class="n">zeros</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用ones()函数定义指定大小和类型的cv::Mat（全为0）
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">ma</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">::</span><span class="n">ones</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用eye()函数定义指定大小和类型的cv::Mat（恒等矩阵）
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">::</span><span class="n">eye</span><span class="p">(</span><span class="kt">int</span> <span class="n">rows</span><span class="p">,</span> <span class="kt">int</span> <span class="n">cols</span><span class="p">,</span> <span class="kt">int</span> <span class="n">type</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="opencv中的数据类型">opencv中的数据类型</h2>
<p>CV_8UC3 代表8位无符号3通道彩色图像</p>
<h2 id="选择图像中指定区域">选择图像中指定区域</h2>
<h2 id="mat类型支持的矩阵运算">Mat类型支持的矩阵运算</h2>
<p><img src="/.img/153531.png"
	
	
	
	loading="lazy"
	
		alt="img"
	
	
></p>
<h2 id="基础数据结构">基础数据结构</h2>
<p>基础图像矩阵容器 cv::Mat
点: cv::Point
颜色: cv::Scalar
大小: cv::Size
矩形: cv::Rect</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="c1">// 点
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">Point</span> <span class="n">point</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">40</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 黑色,蓝色,绿色,红色
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">Scalar</span><span class="p">();</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">255</span><span class="p">);</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">);</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 尺寸
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">Size</span> <span class="nf">size</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="颜色空间转换">颜色空间转换</h2>
<p>参数,输入图像,输出图像,颜色空间转换标识符,颜色通道数(默认为0表示输入输出一致)
<code>void ctvColor(InputArray src, OutputArray dst, int code, int dstCn=0)</code></p>
<h2 id="图形绘制">图形绘制</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="c1">// line(Mat 两个点 颜色)
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">line</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span> <span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">370</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span> <span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">),</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">line</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">370</span><span class="p">,</span> <span class="mi">30</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">370</span><span class="p">,</span> <span class="mi">370</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">),</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// ellipse椭圆 
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">ellipse</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">50</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">50</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Size</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="mf">45.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">360.0</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">            <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">),</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// rectangle矩形
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">rectangle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Rect</span><span class="p">(</span><span class="mi">100</span>   <span class="p">,</span> <span class="mi">100</span>   <span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">rectangle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Rect</span><span class="p">(</span><span class="mi">100</span>   <span class="p">,</span> <span class="mi">300</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">rectangle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Rect</span><span class="p">(</span><span class="mi">300</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">100</span>   <span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">rectangle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Rect</span><span class="p">(</span><span class="mi">300</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">300</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// circle圆形
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">circle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">10</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">circle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">20</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">circle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">30</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">2</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">circle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">40</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">3</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">::</span><span class="n">circle</span><span class="p">(</span><span class="n">mat</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">50</span><span class="p">,</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">),</span> <span class="mi">4</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="c1">// fillPoly多边形填充
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">cv</span><span class="o">::</span><span class="n">Point</span> <span class="n">rootPoints</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">4</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="n">rootPoints</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">150</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">rootPoints</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">250</span><span class="p">,</span> <span class="mi">200</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">rootPoints</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">250</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">rootPoints</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Point</span><span class="p">(</span><span class="mi">150</span><span class="p">,</span> <span class="mi">200</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="cv常用操作">cv常用操作</h2>
<p>计时函数 getTickCount和getTickFrequency
访问像素 at函数, 迭代器, 数据指针
旋转图像 
cv::transpose() 直接顺时针旋转90度
cv::rotate() 三个参数输入图像,输出图像,旋转枚举
<code>cv::rotate(srcMat, srcMat, cv::ROTATE_90_CLOCKWISE);</code>
镜像翻转
<code>cv::flip(srcMat, srcMat, 0); //小于0 xy轴翻转,=0 x轴翻转, 大于0 y轴翻转</code>
缩放 (输入,输出,缩放后大小, x缩放比例, y缩放比例, 差值枚举)
CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
Size dsize, double fx = 0, double fy = 0,
int interpolation = INTER_LINEAR );
enum InterpolationFlags{
INTER_NEAREST        = 0,	// 最临近差值
INTER_LINEAR         = 1,   // 双线性差值
INTER_CUBIC          = 2,   // 双立方差值
INTER_AREA           = 3,
INTER_LANCZOS4       = 4,  // 附近像素及原像素加权取值
INTER_LINEAR_EXACT = 5,
INTER_MAX            = 7,
WARP_FILL_OUTLIERS   = 8,
WARP_INVERSE_MAP     = 16
};</p>
<h2 id="区域图像roi">区域图像ROI</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="n">Mat</span> <span class="nf">img</span><span class="p">(</span><span class="n">SIZE</span><span class="p">(</span><span class="mi">320</span><span class="p">,</span><span class="mi">240</span><span class="p">),</span><span class="n">CV_8UC3</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">Mat</span> <span class="n">roi2</span> <span class="o">=</span> <span class="n">img</span><span class="p">(</span><span class="n">Rect</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="err">当然也可以使用</span><span class="n">Range对象来定义感兴趣区域</span><span class="err">，如下：</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 用括号运算符
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">Mat</span> <span class="n">roi3</span> <span class="o">=</span> <span class="n">img</span><span class="p">(</span><span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">));</span> 
</span></span><span class="line"><span class="cl"><span class="c1">// 用构造函数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">Mat</span> <span class="nf">roi4</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span> <span class="n">Range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 取对角线函数, (1)代表主对角线下方次级对角线
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">img</span><span class="p">.</span><span class="n">diag</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="图像混合">图像混合</h2>
<h2 id="亮度与对比度">亮度与对比度</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="kt">void</span> <span class="n">OpenCVManager</span><span class="o">::</span><span class="n">testContrastAndBrightness</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">mat1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">QString</span> <span class="n">fileName1</span> <span class="o">=</span> <span class="s">&#34;E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/1.jpg&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">mat1</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">imread</span><span class="p">(</span><span class="n">fileName1</span><span class="p">.</span><span class="n">toStdString</span><span class="p">());</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="n">cv</span><span class="o">::</span><span class="n">String</span> <span class="n">windowName</span> <span class="o">=</span> <span class="s">&#34;openCVDemo v1.8.0&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cvui</span><span class="o">::</span><span class="n">init</span><span class="p">(</span><span class="n">windowName</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl">    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">mat1</span><span class="p">.</span><span class="n">data</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">qDebug</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">__FILE__</span> <span class="o">&lt;&lt;</span> <span class="n">__LINE__</span>
</span></span><span class="line"><span class="cl">                 <span class="o">&lt;&lt;</span> <span class="s">&#34;Failed to load image:&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">fileName1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 增强对比度
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">float</span> <span class="n">r</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">float</span> <span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">float</span> <span class="n">b</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">dstMat</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">dstMat</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="o">::</span><span class="n">zeros</span><span class="p">(</span><span class="n">mat1</span><span class="p">.</span><span class="n">size</span><span class="p">(),</span> <span class="n">mat1</span><span class="p">.</span><span class="n">type</span><span class="p">());</span>
</span></span><span class="line"><span class="cl">    <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">windowMat</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span><span class="p">(</span><span class="n">cv</span><span class="o">::</span><span class="n">Size</span><span class="p">(</span><span class="n">dstMat</span><span class="p">.</span><span class="n">cols</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">rows</span><span class="p">),</span> <span class="n">CV_8UC3</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">alpha</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span>    <span class="c1">// 小于1，则降低对比度
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">int</span> <span class="n">beta</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>     <span class="c1">// 负数，则降低亮度
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cvui</span><span class="o">::</span><span class="n">window</span><span class="p">(</span><span class="n">windowMat</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">cols</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">cols</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">rows</span><span class="p">,</span> <span class="s">&#34;settings&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">while</span><span class="p">(</span><span class="nb">true</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">windowMat</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">Scalar</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">cvui</span><span class="o">::</span><span class="n">printf</span><span class="p">(</span><span class="n">windowMat</span><span class="p">,</span> <span class="mi">375</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="s">&#34;contrast&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">cvui</span><span class="o">::</span><span class="n">trackbar</span><span class="p">(</span><span class="n">windowMat</span><span class="p">,</span> <span class="mi">375</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">165</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">alpha</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">400</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">cvui</span><span class="o">::</span><span class="n">printf</span><span class="p">(</span><span class="n">windowMat</span><span class="p">,</span> <span class="mi">375</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="s">&#34;brightness&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">cvui</span><span class="o">::</span><span class="n">trackbar</span><span class="p">(</span><span class="n">windowMat</span><span class="p">,</span> <span class="mi">375</span><span class="p">,</span> <span class="mi">110</span><span class="p">,</span> <span class="mi">165</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">beta</span><span class="p">,</span> <span class="o">-</span><span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="cp">#if 1
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>        <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">row</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">row</span> <span class="o">&lt;</span> <span class="n">mat1</span><span class="p">.</span><span class="n">rows</span><span class="p">;</span> <span class="n">row</span><span class="o">++</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">col</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">col</span> <span class="o">&lt;</span> <span class="n">mat1</span><span class="p">.</span><span class="n">cols</span><span class="p">;</span> <span class="n">col</span><span class="o">++</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">            <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">b</span> <span class="o">=</span> <span class="n">mat1</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">0</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">                <span class="n">g</span> <span class="o">=</span> <span class="n">mat1</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">1</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">                <span class="n">r</span> <span class="o">=</span> <span class="n">mat1</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">2</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="cp">#if 0</span><span class="c">
</span></span></span><span class="line"><span class="cl"><span class="c">                // 改变背景色
</span></span></span><span class="line"><span class="cl"><span class="c">                if(r &gt; 200 &amp;&amp; g &gt; 200 &amp;&amp; b &gt; 200)
</span></span></span><span class="line"><span class="cl"><span class="c">                {
</span></span></span><span class="line"><span class="cl"><span class="c">                    r = 25/4.0;
</span></span></span><span class="line"><span class="cl"><span class="c">                    g = 246.0f;
</span></span></span><span class="line"><span class="cl"><span class="c">                    b = 197.0f;
</span></span></span><span class="line"><span class="cl"><span class="c">                    dstMat.at&lt;cv::Vec3b&gt;(row, col)[0] = b;
</span></span></span><span class="line"><span class="cl"><span class="c">                    dstMat.at&lt;cv::Vec3b&gt;(row, col)[1] = g;
</span></span></span><span class="line"><span class="cl"><span class="c">                    dstMat.at&lt;cv::Vec3b&gt;(row, col)[2] = r;
</span></span></span><span class="line"><span class="cl"><span class="c">                }
</span></span></span><span class="line"><span class="cl"><span class="c"></span><span class="cp">#endif
</span></span></span><span class="line"><span class="cl"><span class="cp">#if 1
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>                <span class="c1">// 对比度、亮度计算公式 cv::saturate_cast&lt;uchar&gt;(value)：防止溢出
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">b</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">g</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">r</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="cp">#else
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>                <span class="c1">// 对比度、亮度计算公式 cv::saturate_cast&lt;uchar&gt;(value)：防止溢出
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">b</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">g</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">                <span class="n">dstMat</span><span class="p">.</span><span class="n">at</span><span class="o">&lt;</span><span class="n">cv</span><span class="o">::</span><span class="n">Vec3b</span><span class="o">&gt;</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">cv</span><span class="o">::</span><span class="n">saturate_cast</span><span class="o">&lt;</span><span class="n">uchar</span><span class="o">&gt;</span><span class="p">(</span><span class="n">r</span> <span class="o">*</span> <span class="n">alpha</span> <span class="o">/</span> <span class="mf">100.0f</span> <span class="o">+</span> <span class="n">beta</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="cp">#endif
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>            <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">//        cvui::trackbar(windowMat, dstMat.cols, dstMat.rows / 3, dstMat.cols, &amp;?alpha, 0, 100, );
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">cv</span><span class="o">::</span><span class="n">Mat</span> <span class="n">imageMat</span> <span class="o">=</span> <span class="n">windowMat</span><span class="p">(</span><span class="n">cv</span><span class="o">::</span><span class="n">Range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">rows</span><span class="p">),</span> <span class="n">cv</span><span class="o">::</span><span class="n">Range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">.</span><span class="n">cols</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">        <span class="n">cv</span><span class="o">::</span><span class="n">addWeighted</span><span class="p">(</span><span class="n">imageMat</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">dstMat</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0f</span><span class="p">,</span> <span class="n">imageMat</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="cp">#endif
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>        <span class="n">cvui</span><span class="o">::</span><span class="n">update</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">cv</span><span class="o">::</span><span class="n">imshow</span><span class="p">(</span><span class="n">windowName</span><span class="p">,</span> <span class="n">windowMat</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="n">cv</span><span class="o">::</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span> <span class="o">==</span> <span class="mi">27</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">break</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h1 id="pythoncv">pythoncv</h1>
<h2 id="图像读取">图像读取</h2>
<p>读取 imread(path,IMREAD_COLOR)
修改图像cvtColor(原图像,目标图像,转换格式) //的功能是把图像从一个彩色空间转换到另外一个色彩空间
保存图像imgwrite()</p>
<h2 id="图像操作">图像操作</h2>
<p>addWeighted(img,1-res,new_img,res,0.0,dest);</p>
<h1 id="python">python</h1>
<h3 id="读取图像">读取图像</h3>
<p>cv2.imread()</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">img</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s1">&#39;messi5.jpg&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>cv2.IMREAD_COLOR：读入一副彩色图像。图像的透明度会被忽略， 这是默认参数。 •</li>
<li>cv2.IMREAD_GRAYSCALE：以灰度模式读入图像</li>
<li>cv2.IMREAD_UNCHANGED：读入一幅图像，并且包括图像的 alpha 通道</li>
</ul>
<h3 id="显示图像">显示图像</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="n">img</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="c1">#如果你用的是 64 位系统，你需要将  k = cv2.waitKey(0) 这行改成k = cv2.waitKey(0)&amp;0xFF。</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">destroyAllWindows</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>cv2.waitKey() 是一个键盘绑定函数。需要指出的是它的时间尺度是毫 秒级。函数等待特定的几毫秒，看是否有键盘输入。特定的几毫秒之内，如果 按下任意键，这个函数会返回按键的 ASCII 码值，程序将会继续运行。如果没 有键盘输入，返回值为 -1，如果我们设置这个函数的参数为 0，那它将会无限 期的等待键盘输入。它也可以被用来检测特定键是否被按下，例如按键 a 是否 被按下，这个后面我们会接着讨论。</li>
<li>cv2.destroyAllWindows() 可以轻易删除任何我们建立的窗口。如果 你想删除特定的窗口可以使用 cv2.destroyWindow()，在括号内输入你想删 除的窗口名。</li>
</ul>
<h3 id="保存图像">保存图像</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">imwrite</span><span class="p">(</span><span class="s1">&#39;messigray.png&#39;</span><span class="p">,</span><span class="n">img</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="使用-matplotlib">使用 Matplotlib</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">cmap</span> <span class="o">=</span> <span class="s1">&#39;gray&#39;</span><span class="p">,</span> <span class="n">interpolation</span> <span class="o">=</span> <span class="s1">&#39;bicubic&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">xticks</span><span class="p">([]),</span> <span class="n">plt</span><span class="o">.</span><span class="n">yticks</span><span class="p">([])</span> <span class="c1"># to hide tick values on X and Y axis</span>
</span></span><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>
<p>彩色图像使用 OpenCV 加载时是 BGR 模式。但是 Matplotib 是 RGB 模式。所以彩色图像如果已经被 OpenCV 读取，那它将不会被 Matplotib 正 确显示。要采用以下方法</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">img</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s1">&#39;messi4.jpg&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">b</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">r</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">img2</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">merge</span><span class="p">([</span><span class="n">r</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">b</span><span class="p">])</span> <span class="c1"># 将bgr合并为rgb</span>
</span></span><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">121</span><span class="p">);</span><span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="p">)</span> <span class="c1"># expects distorted color</span>
</span></span><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">122</span><span class="p">);</span><span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">img2</span><span class="p">)</span> <span class="c1"># expect true color</span>
</span></span><span class="line"><span class="cl"><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;bgr image&#39;</span><span class="p">,</span><span class="n">img</span><span class="p">)</span> <span class="c1"># expects true color</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;rgb image&#39;</span><span class="p">,</span><span class="n">img2</span><span class="p">)</span> <span class="c1"># expects distorted color</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>或者使用**<code>img2 = img[:,:,::-1]</code>**</p>
</li>
</ul>
<h3 id="用摄像头捕获视频">用摄像头捕获视频</h3>
<ul>
<li>
<p>创建VideoCapture :  <code>cap = cv2.VideoCapture(0)</code> 参数是 设备的索引号，或者是一个视频文件。设备索引号就是在指定要使用的摄像头。 一般的笔记本电脑都有内置摄像头。所以参数就是 0。你可以通过设置成 1 或 者其他的来选择别的摄像头</p>
</li>
<li>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">cap</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">VideoCapture</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="kc">True</span><span class="p">):</span>
</span></span><span class="line"><span class="cl"><span class="c1"># Capture frame-by-frame</span>
</span></span><span class="line"><span class="cl">    <span class="n">ret</span><span class="p">,</span> <span class="n">frame</span> <span class="o">=</span> <span class="n">cap</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="c1"># 当读取成功ret返回True</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># Our operations on the frame come here</span>
</span></span><span class="line"><span class="cl">    <span class="n">gray</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">cvtColor</span><span class="p">(</span><span class="n">frame</span><span class="p">,</span> <span class="n">cv2</span><span class="o">.</span><span class="n">COLOR_BGR2GRAY</span><span class="p">)</span><span class="c1"># </span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># Display the resulting frame</span>
</span></span><span class="line"><span class="cl">    <span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;frame&#39;</span><span class="p">,</span><span class="n">gray</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="n">cv2</span><span class="o">.</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xFF</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;q&#39;</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="k">break</span>
</span></span><span class="line"><span class="cl"><span class="c1"># When everything done, release the capture</span>
</span></span><span class="line"><span class="cl"><span class="n">cap</span><span class="o">.</span><span class="n">release</span><span class="p">()</span> <span class="c1"># 释放资源</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">destroyAllWindows</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>有时 cap 可能不能成功的初始化摄像头设备。这种情况下上面的代码会报 错。你可以使用 cap.isOpened()，来检查是否成功初始化了。如果返回值是 True，那就没有问题。否则就要使用函数 cap.open()。</p>
</li>
<li>
<p>你可以使用函数 cap.get(propId) 来获得视频的一些参数信息。这里 propId 可以是 0 到 18 之间的任何整数。每一个数代表视频的一个属性，见 下表</p>
</li>
<li>
<p>• CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds. • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next. • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film. • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream. • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream. • CV_CAP_PROP_FPS Frame rate. • CV_CAP_PROP_FOURCC 4-character code of codec. • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file. • CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() . • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode. • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras). • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras). • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras). • CV_CAP_PROP_HUE Hue of the image (only for cameras). • CV_CAP_PROP_GAIN Gain of the image (only for cameras). • CV_CAP_PROP_EXPOSURE Exposure (only for cameras). • CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB. • CV_CAP_PROP_WHITE_BALANCE Currently unsupported • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently) 2</p>
</li>
</ul>
<h3 id="从文件中播放视频">从文件中播放视频</h3>
<p>与从摄像头中捕获一样，你只需要把设备索引号改成视频文件的名字。在 播放每一帧时，使用 cv2.waiKey() 设置适当的持续时间。如果设置的太低视 频就会播放的非常快，如果设置的太高就会播放的很慢（你可以使用这种方法 控制视频的播放速度）。通常情况下 25 毫秒就可以了。</p>
<h3 id="保存视频">保存视频</h3>
<p>用<code>cv2.cv.FOURCC(*'XVID')</code>或者<code>fourcc = cv2.VideoWriter_fourcc(*'XVID')</code>指定fourcc , FourCC 就是一个 4 字节码，用来确定视频的编码格式。</p>
<p>常用的FourCC:</p>
<p>In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video) • In Windows: DIVX (More to be tested and added)</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="mi">11</span> <span class="n">cap</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">VideoCapture</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># 将0改为视频文件地址可以读取视频文件</span>
</span></span><span class="line"><span class="cl"><span class="mi">12</span>
</span></span><span class="line"><span class="cl"><span class="mi">13</span> <span class="c1"># 创建fourcc对象,并指定</span>
</span></span><span class="line"><span class="cl"><span class="mi">14</span> <span class="n">fourcc</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">VideoWriter_fourcc</span><span class="p">(</span><span class="o">*</span><span class="s1">&#39;XVID&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="mi">15</span> <span class="n">out</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">VideoWriter</span><span class="p">(</span><span class="s1">&#39;output.avi&#39;</span><span class="p">,</span><span class="n">fourcc</span><span class="p">,</span> <span class="mf">20.0</span><span class="p">,</span> <span class="p">(</span><span class="mi">640</span><span class="p">,</span><span class="mi">480</span><span class="p">),</span><span class="kc">True</span><span class="p">)</span><span class="c1">#创建文件实例,确定播放频率和帧的大小</span>
</span></span><span class="line"><span class="cl"><span class="mi">16</span><span class="c1">#最后一个是 isColor 标签。如果是 True，每一帧就是彩色图，否则就是灰度图</span>
</span></span><span class="line"><span class="cl"><span class="mi">17</span> <span class="k">while</span><span class="p">(</span><span class="n">cap</span><span class="o">.</span><span class="n">isOpened</span><span class="p">()):</span>
</span></span><span class="line"><span class="cl">         <span class="n">ret</span><span class="p">,</span> <span class="n">frame</span> <span class="o">=</span> <span class="n">cap</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">         <span class="k">if</span> <span class="n">ret</span><span class="o">==</span><span class="kc">True</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     		<span class="n">frame</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">flip</span><span class="p">(</span><span class="n">frame</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    
</span></span><span class="line"><span class="cl">    	 <span class="c1"># write the flipped frame</span>
</span></span><span class="line"><span class="cl">    	 <span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span> <span class="c1"># 调用实例保存视频文件</span>
</span></span><span class="line"><span class="cl">    
</span></span><span class="line"><span class="cl">    	 <span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;frame&#39;</span><span class="p">,</span><span class="n">frame</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     		<span class="k">if</span> <span class="n">cv2</span><span class="o">.</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xFF</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="s1">&#39;q&#39;</span><span class="p">):</span><span class="c1"># waitkey函数返回输入的键盘ASCII值,单位是ms</span>
</span></span><span class="line"><span class="cl">     			<span class="k">break</span>
</span></span><span class="line"><span class="cl">     	<span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     		<span class="k">break</span>
</span></span><span class="line"><span class="cl"><span class="mi">30</span>
</span></span><span class="line"><span class="cl"><span class="mi">31</span> <span class="c1"># Release everything if job is finished</span>
</span></span><span class="line"><span class="cl"><span class="mi">32</span> <span class="n">cap</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="mi">33</span> <span class="n">out</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="mi">34</span> <span class="n">cv2</span><span class="o">.</span><span class="n">destroyAllWindows</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="opencv中的绘图函数">OpenCV中的绘图函数</h2>
<p>画线cv2.line()，画圆cv2.circle()，画矩形cv2.rectangle()， 画椭圆cv2.ellipse()，</p>
<p>添加文字: cv2.putText()</p>
<p>画多边形: cv2.reshape</p>
<p>上面所有的这些绘图函数需要设置下面这些参数：</p>
<ul>
<li>
<p>img：你想要绘制图形的那幅图像。</p>
</li>
<li>
<p>color：形状的颜色。以 RGB 为例，需要传入一个元组，例如, (255,0,0) 代表蓝色。对于灰度图只需要传入灰度值。</p>
</li>
<li>
<p>thickness：线条的粗细。如果给一个闭合图形设置为 -1，那么这个图形 就会被填充。默认值是 1.</p>
</li>
<li>
<p>linetype：线条的类型，8 连接，抗锯齿等。默认情况是 8 连接。cv2.LINE_AA 为抗锯齿，这样看起来会非常平滑。</p>
</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1"># Create a black image</span>
</span></span><span class="line"><span class="cl"><span class="n">img</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">512</span><span class="p">,</span><span class="mi">512</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># Draw a diagonal blue line with thickness of 5 px</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">line</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="mi">511</span><span class="p">,</span><span class="mi">511</span><span class="p">),(</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">5</span><span class="p">,</span><span class="n">cv2</span><span class="o">.</span><span class="n">LINE_AA</span><span class="p">)</span> <span class="c1"># 第二个和第三个参数分别为起始坐标和结束坐标</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 画矩形</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="mi">384</span><span class="p">,</span><span class="mi">0</span><span class="p">),(</span><span class="mi">510</span><span class="p">,</span><span class="mi">128</span><span class="p">),(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">3</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 画圆 参数分别为中心坐标点和半径大小</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">circle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="mi">447</span><span class="p">,</span><span class="mi">63</span><span class="p">),</span> <span class="mi">63</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 画椭圆 第一个参数是中心点坐标,第二个为长轴短轴的长度,angle是椭圆沿逆时针方向旋转的角度。startAngle和endAngle表示从主轴沿顺时针方向测量的椭圆弧的开始和结束</span>
</span></span><span class="line"><span class="cl"><span class="n">cv2</span><span class="o">.</span><span class="n">ellipse</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="mi">256</span><span class="p">,</span><span class="mi">256</span><span class="p">),(</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">),</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">180</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 画绘制多边形，首先需要顶点的坐标。将这些点组成形状为 ROWSx1x2 的数组，其中 ROWS 是顶点数，并且其类型应为int32。在这里，我们绘制了一个带有四个顶点的黄色小多边形。</span>
</span></span><span class="line"><span class="cl"><span class="n">pts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">10</span><span class="p">,</span><span class="mi">5</span><span class="p">],[</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">],[</span><span class="mi">70</span><span class="p">,</span><span class="mi">20</span><span class="p">],[</span><span class="mi">50</span><span class="p">,</span><span class="mi">10</span><span class="p">]],</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span><span class="c1">#顶点坐标</span>
</span></span><span class="line"><span class="cl"><span class="n">pts</span> <span class="o">=</span> <span class="n">pts</span><span class="o">.</span><span class="n">reshape</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span><span class="c1"># </span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">polylines</span><span class="p">(</span><span class="n">img</span><span class="p">,[</span><span class="n">pts</span><span class="p">],</span><span class="kc">True</span><span class="p">,(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 注意 如果第三个参数为False，您将获得一条连接所有点的折线，而不是闭合形状。cv.polylines()可用于绘制多条线。只需创建要绘制的所有线条的列表，然后将其传递给函数即可。所有线条将单独绘制。与为每条线调用**cv.line**相比，绘制一组线是一种更好，更快的方法。</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="鼠标移动函数">鼠标移动函数</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cv2</span> <span class="k">as</span> <span class="nn">cv</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 列出所有可用的事件</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cv2</span> <span class="k">as</span> <span class="nn">cv</span>
</span></span><span class="line"><span class="cl"><span class="n">events</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">cv</span><span class="p">)</span> <span class="k">if</span> <span class="s1">&#39;EVENT&#39;</span> <span class="ow">in</span> <span class="n">i</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span> <span class="n">events</span> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 鼠标回调函数</span>
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">draw_circle</span><span class="p">(</span><span class="n">event</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">flags</span><span class="p">,</span><span class="n">param</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">     <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="n">cv</span><span class="o">.</span><span class="n">EVENT_LBUTTONDBLCLK</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     <span class="n">cv</span><span class="o">.</span><span class="n">circle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),</span><span class="mi">100</span><span class="p">,(</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 创建一个黑色的图像，一个窗口，并绑定到窗口的功能</span>
</span></span><span class="line"><span class="cl"><span class="n">img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">512</span><span class="p">,</span><span class="mi">512</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">namedWindow</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">setMouseCallback</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="n">draw_circle</span><span class="p">)</span><span class="c1">#</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">     <span class="n">cv</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="n">img</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="k">if</span> <span class="n">cv</span><span class="o">.</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xFF</span> <span class="o">==</span> <span class="mi">27</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">        <span class="k">break</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">destroyAllWindows</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cv2</span> <span class="k">as</span> <span class="nn">cv</span>
</span></span><span class="line"><span class="cl"><span class="n">drawing</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># 如果按下鼠标，则为真</span>
</span></span><span class="line"><span class="cl"><span class="n">mode</span> <span class="o">=</span> <span class="kc">True</span> <span class="c1"># 如果为真，绘制矩形。按 m 键可以切换到曲线</span>
</span></span><span class="line"><span class="cl"><span class="n">ix</span><span class="p">,</span><span class="n">iy</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 鼠标回调函数</span>
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">draw_circle</span><span class="p">(</span><span class="n">event</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">flags</span><span class="p">,</span><span class="n">param</span><span class="p">):</span>
</span></span><span class="line"><span class="cl"> <span class="k">global</span> <span class="n">ix</span><span class="p">,</span><span class="n">iy</span><span class="p">,</span><span class="n">drawing</span><span class="p">,</span><span class="n">mode</span>
</span></span><span class="line"><span class="cl"> <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="n">cv</span><span class="o">.</span><span class="n">EVENT_LBUTTONDOWN</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 	<span class="n">drawing</span> <span class="o">=</span> <span class="kc">True</span>
</span></span><span class="line"><span class="cl"> 	<span class="n">ix</span><span class="p">,</span><span class="n">iy</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span>
</span></span><span class="line"><span class="cl"> <span class="k">elif</span> <span class="n">event</span> <span class="o">==</span> <span class="n">cv</span><span class="o">.</span><span class="n">EVENT_MOUSEMOVE</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 	<span class="k">if</span> <span class="n">drawing</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 		<span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 			<span class="n">cv</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">ix</span><span class="p">,</span><span class="n">iy</span><span class="p">),(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> 		<span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 			<span class="n">cv</span><span class="o">.</span><span class="n">circle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),</span><span class="mi">5</span><span class="p">,(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">),</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"> <span class="k">elif</span> <span class="n">event</span> <span class="o">==</span> <span class="n">cv</span><span class="o">.</span><span class="n">EVENT_LBUTTONUP</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 	<span class="n">drawing</span> <span class="o">=</span> <span class="kc">False</span>
</span></span><span class="line"><span class="cl"> 	<span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 		<span class="n">cv</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">ix</span><span class="p">,</span><span class="n">iy</span><span class="p">),(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"> 		<span class="n">cv</span><span class="o">.</span><span class="n">circle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),</span><span class="mi">5</span><span class="p">,(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">),</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="轨迹栏作调色板按钮">轨迹栏作调色板,按钮</h3>
<p><strong>cv.getTrackbarPos</strong>，<strong>cv.createTrackbar</strong></p>
<p>对于 cv.getTrackbarPos() 函数，第一个参数是轨迹栏名称，第二个参数是它附加到的窗口名 称，第三个参数是默认值，第四个参数是最大值，第五个是执行的回调函数每次跟踪栏值更改。 回调函数始终具有默认参数，即轨迹栏位置。在我们的例子中，函数什么都不做，所以我们简单 地通过。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cv2</span> <span class="k">as</span> <span class="nn">cv</span>
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">nothing</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
</span></span><span class="line"><span class="cl"> <span class="k">pass</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 创建一个黑色的图像，一个窗口</span>
</span></span><span class="line"><span class="cl"><span class="n">img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">300</span><span class="p">,</span><span class="mi">512</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">namedWindow</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 创建颜色变化的轨迹栏</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 第一个参数是轨迹栏名称，第二个参数是它附加到的窗口名 称，第三个参数是默认值，第四个参数是最大值，第五个是执行的回调函数</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">createTrackbar</span><span class="p">(</span><span class="s1">&#39;R&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="n">nothing</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">createTrackbar</span><span class="p">(</span><span class="s1">&#39;G&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="n">nothing</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">createTrackbar</span><span class="p">(</span><span class="s1">&#39;B&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="n">nothing</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 为 ON/OFF 功能创建开关</span>
</span></span><span class="line"><span class="cl"><span class="n">switch</span> <span class="o">=</span> <span class="s1">&#39;0 : OFF </span><span class="se">\n</span><span class="s1">1 : ON&#39;</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">createTrackbar</span><span class="p">(</span><span class="n">switch</span><span class="p">,</span> <span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">nothing</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">     <span class="n">cv</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s1">&#39;image&#39;</span><span class="p">,</span><span class="n">img</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="n">k</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">waitKey</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">&amp;</span> <span class="mh">0xFF</span>
</span></span><span class="line"><span class="cl">     <span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="mi">27</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     	<span class="k">break</span>
</span></span><span class="line"><span class="cl">     <span class="c1"># 得到四条轨迹的当前位置</span>
</span></span><span class="line"><span class="cl">     <span class="n">r</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">getTrackbarPos</span><span class="p">(</span><span class="s1">&#39;R&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="n">g</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">getTrackbarPos</span><span class="p">(</span><span class="s1">&#39;G&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="n">b</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">getTrackbarPos</span><span class="p">(</span><span class="s1">&#39;B&#39;</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="n">s</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">getTrackbarPos</span><span class="p">(</span><span class="n">switch</span><span class="p">,</span><span class="s1">&#39;image&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">     <span class="k">if</span> <span class="n">s</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     	<span class="n">img</span><span class="p">[:]</span> <span class="o">=</span> <span class="mi">0</span>
</span></span><span class="line"><span class="cl">     <span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">     	<span class="n">img</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[</span><span class="n">b</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">r</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="n">cv</span><span class="o">.</span><span class="n">destroyAllWindows</span><span class="p">()</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><img src="/C:%5cUsers%5cMeruro%5cAppData%5cRoaming%5cTypora%5ctypora-user-images%5cimage-20200804124826005.png"
	
	
	
	loading="lazy"
	
		alt="image-20200804124826005"
	
	
></p>
<h2 id="图像的基本操作">图像的基本操作</h2>
<h3 id="访问和修改像素值">访问和修改像素值</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">img</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s1">&#39;messi5.jpg&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">px</span> <span class="o">=</span> <span class="n">img</span><span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">100</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span> <span class="n">px</span> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">[</span><span class="mi">157</span> <span class="mi">166</span> <span class="mi">200</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 仅访问蓝色像素</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">blue</span> <span class="o">=</span> <span class="n">img</span><span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">100</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span> <span class="n">blue</span> <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="mi">157</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 修改像素值</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">img</span><span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">100</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 更好的办法</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">img</span><span class="o">.</span><span class="n">itemset</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="mi">100</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="访问图像属性">访问图像属性</h3>
<p>图像的形状可通过 img.shape 访问。它返回行，列和通道数的元组（如果图像是彩色的）</p>
<p>像素总数可通过访问 img.size</p>
<p>图像数据类型通过 img.dtype 获得</p>
<h3 id="图像感兴趣区域-roi">图像感兴趣区域 (ROI)</h3>
<p>ROI就是一个区域</p>
<p>ball = img[280:340, 330:390] 获取图像的一个区域</p>
<h3 id="拆分和合并图像通道">拆分和合并图像通道</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1"># 使用函数</span>
</span></span><span class="line"><span class="cl"><span class="n">b</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">r</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">img</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">img</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">merge</span><span class="p">((</span><span class="n">b</span><span class="p">,</span><span class="n">g</span><span class="p">,</span><span class="n">r</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 使用numpy索引</span>
</span></span><span class="line"><span class="cl"><span class="n">b</span> <span class="o">=</span> <span class="n">img</span> <span class="p">[:,</span> <span class="p">:,</span> <span class="mi">0</span><span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="n">img</span> <span class="p">[:,</span> <span class="p">:,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>cv.split() 是一项耗时的操作（就时间而言）。因此，仅在必要时才这样做。否则请进行Numpy 索引。</p>
<h3 id="图像加法运算">图像加法运算</h3>
<p>您可以通过OpenCV函数 cv.add() 或仅通过numpy操作 res = img1 + img2 添加两个图像。两个 图像应具有相同的深度和类型，或者第二个图像可以只是一个标量值。</p>
<p>注意 OpenCV加法和Numpy加法之间有区别。OpenCV加法是饱和运算，而Numpy加法是模运 算。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">([</span><span class="mi">250</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">([</span><span class="mi">10</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 饱和运算</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span> <span class="n">cv</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="p">)</span> <span class="c1"># 250+10 = 260 =&gt; 255</span>
</span></span><span class="line"><span class="cl"><span class="p">[[</span><span class="mi">255</span><span class="p">]]</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 模运算</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="nb">print</span><span class="p">(</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span> <span class="p">)</span> <span class="c1"># 250+10 = 260 % 256 = 4</span>
</span></span><span class="line"><span class="cl"><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="图像融合">图像融合</h3>
<p>dst = cv.addWeighted(img1,0.7,img2,0.3,0)</p>
<h3 id="按位运算-">按位运算 ?</h3>
<h3 id="使用opencv衡量性能">使用OpenCV衡量性能</h3>
<p>cv.getTickCount，<strong>cv.getTickFrequency</strong>。</p>

</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/%E4%BB%A3%E7%A0%81/">代码</a>
        
    </section>


    
    <section class="article-copyright">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copyright" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <path d="M14.5 9a3.5 4 0 1 0 0 6" />
</svg>



        <span>Licensed under CC BY-NC-SA 4.0</span>
    </section>
    </footer>


    
</article>

    

    

     
    
        
    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2020 - 
        
        2023 梅鲁梅鲁梅鲁梅~
    </section>
    
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        Theme <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.13.0">Stack</a></b> designed by <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a>
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
    <aside class="sidebar right-sidebar sticky">
        
            
                
    <section class="widget archives">
        <div class="widget-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



        </div>
        <h2 class="widget-title section-title">Table of contents</h2>
        
        <div class="widget--toc">
            <nav id="TableOfContents">
  <ol>
    <li><a href="#基础目录结构">基础目录结构</a></li>
    <li><a href="#模态对话框和非模态对话框">模态对话框和非模态对话框</a></li>
    <li><a href="#信号与槽">信号与槽</a></li>
  </ol>

  <ol>
    <li><a href="#编译opencv">编译opencv</a></li>
    <li><a href="#cvmat-mat初始化">cv::Mat mat初始化</a></li>
    <li><a href="#opencv中的数据类型">opencv中的数据类型</a></li>
    <li><a href="#选择图像中指定区域">选择图像中指定区域</a></li>
    <li><a href="#mat类型支持的矩阵运算">Mat类型支持的矩阵运算</a></li>
    <li><a href="#基础数据结构">基础数据结构</a></li>
    <li><a href="#颜色空间转换">颜色空间转换</a></li>
    <li><a href="#图形绘制">图形绘制</a></li>
    <li><a href="#cv常用操作">cv常用操作</a></li>
    <li><a href="#区域图像roi">区域图像ROI</a></li>
    <li><a href="#图像混合">图像混合</a></li>
    <li><a href="#亮度与对比度">亮度与对比度</a></li>
  </ol>

  <ol>
    <li><a href="#图像读取">图像读取</a></li>
    <li><a href="#图像操作">图像操作</a></li>
  </ol>

  <ol>
    <li>
      <ol>
        <li><a href="#读取图像">读取图像</a></li>
        <li><a href="#显示图像">显示图像</a></li>
        <li><a href="#保存图像">保存图像</a></li>
        <li><a href="#使用-matplotlib">使用 Matplotlib</a></li>
        <li><a href="#用摄像头捕获视频">用摄像头捕获视频</a></li>
        <li><a href="#从文件中播放视频">从文件中播放视频</a></li>
        <li><a href="#保存视频">保存视频</a></li>
      </ol>
    </li>
    <li><a href="#opencv中的绘图函数">OpenCV中的绘图函数</a>
      <ol>
        <li><a href="#鼠标移动函数">鼠标移动函数</a></li>
        <li><a href="#轨迹栏作调色板按钮">轨迹栏作调色板,按钮</a></li>
      </ol>
    </li>
    <li><a href="#图像的基本操作">图像的基本操作</a>
      <ol>
        <li><a href="#访问和修改像素值">访问和修改像素值</a></li>
        <li><a href="#访问图像属性">访问图像属性</a></li>
        <li><a href="#图像感兴趣区域-roi">图像感兴趣区域 (ROI)</a></li>
        <li><a href="#拆分和合并图像通道">拆分和合并图像通道</a></li>
        <li><a href="#图像加法运算">图像加法运算</a></li>
        <li><a href="#图像融合">图像融合</a></li>
        <li><a href="#按位运算-">按位运算 ?</a></li>
        <li><a href="#使用opencv衡量性能">使用OpenCV衡量性能</a></li>
      </ol>
    </li>
  </ol>
</nav>
        </div>
    </section>

            
        
    </aside>


        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

    </body>
</html>
